--[[
	
	Name: Advanced Questbook
	By: Crypton

	One of these days I might recode most of this addon to reuse code rather than
	have the same code all over the place.. for now bleh.. not too worried about
	it yet. Leave that for "polishing" when there is not much else to expand on.

]]
function AdvQuestBook_ClearIcons()
	local i;
	local j;
	local icon;
	-- Kill all main icons with 1 loop...
	for i = 1, 30 do
		icon = getglobal("AdvQuestBookMapFinIcons"..i);
		icon:Hide();
		icon = getglobal("AdvQuestBookMapIncIcons"..i);
		icon:Hide();
		icon = getglobal("AdvQuestBookMapIndexIcons"..i);
		icon:Hide();
		--[[ Begin Shared Support ]]
		icon = getglobal("AdvQuestBookMapSharedIcons"..i);
		icon:Hide();
		--[[ End Shared Support ]]
	end
	if (AQB_OPEN_ITEMS > 0) then
		for i = 1, AQB_OPEN_ITEMS do
			icon = getglobal("AdvQuestBookMapItemIcons"..i)
			icon:Hide();
		end
	end
	--[[ Begin Shared Support ]]
	if (AQB_SOPEN_ITEMS > 0) then
		for i = 1, AQB_SOPEN_ITEMS do
			icon = getglobal("AdvQuestBookMapsItemIcons"..i)
			icon:Hide();
		end
	end
	--[[ End Shared Support ]]
	icon = getglobal("AdvQuestBookSnoopIconsa");
	icon:Hide();
	icon = getglobal("AdvQuestBookSnoopIconsb");
	icon:Hide();
	AQB_CUR_SNOOP = {};
	for i = 1, 2 do
		icon = getglobal("AdvQuestBookEliteTrainerIcons"..i)
		icon:Hide();
	end
	AQB_FIN_MAPLIST = {};
	AQB_INC_MAPLIST = {};
	AQB_ITEM_INDEX = {};
	AQB_ITEM_DATA = {};
	--[[ Begin Shared Support ]]
	AQB_SITEM_INDEX = {};
	AQB_SITEM_DATA = {};
	AQB_SOPEN_ITEMS = 0;
	--[[ End Shared Support ]]
	AQB_FINICON_COUNT = 0;
	AQB_INCICON_COUNT = 0;
	AQB_OPEN_ITEMS = 0;
	AQB_ENABLE_ICONS = false;
	AQB_LAST_MAP = -1;
	return;
end

function AdvQuestBookMapperWorldMap_IconOnLeave(this)
	if (not AdvQuestBook_Config["settings"][2]["value"]) then
		GameTooltip:Hide();
	end
	if (not AdvQuestBook_Config["settings"][3]["value"]) then
		if (AQB_OPEN_ITEMS > 0) then
			for i = 1, AQB_OPEN_ITEMS do
				icon = getglobal("AdvQuestBookMapItemIcons"..i)
				icon:Hide();
				AQB_OPEN_ITEMS = 0;
			end
		end
	end
end

function AdvQuestBookEliteTrainer_OnEnter(this)
	local icon = getglobal(this);
	GameTooltip:SetOwner(this, "TOPLEFT" and "ANCHOR_LEFT", -6, 0 or "TOPRIGHT" and "ANCHOR_RIGHT", 0, 6);
	GameTooltip:SetText("|cffFFFF00"..icon.name.."|r");
	GameTooltip:AddLine("\n"..icon.items);
	GameTooltip:AddLine("\n"..AQB_ISTRINGS["AQB_COORDS"]..": "..AQB_DecMagic(icon.x, 2)..", "..AQB_DecMagic(icon.y, 2));
	AdvQuestBook_Checktooltip();
	GameTooltip:Show();
end

function AdvQuestBookItemInfo_OnEnter(this)
	local iconnum = string.gsub(this, "AdvQuestBookMapItemIcons", "");
	iconnum = tonumber(iconnum);
	GameTooltip:SetOwner(this, "TOPLEFT" and "ANCHOR_LEFT", -6, 0 or "TOPRIGHT" and "ANCHOR_RIGHT", 0, 6);
	GameTooltip:SetText("|cffFFFF00"..AQB_ITEM_DATA[iconnum]["name"].."|r");
	if (AdvQuestBook_Config["settings"][7]["value"]) then
		GameTooltip:AddLine(AQB_ISTRINGS["AQB_COORDS"]..": "..AQB_DecMagic(AQB_ITEM_DATA[iconnum]["x"], 2)..", "..AQB_DecMagic(AQB_ITEM_DATA[iconnum]["y"], 2));
	end
	AdvQuestBook_Checktooltip();
	GameTooltip:Show();
end

--[[ Begin Shared Support ]]
function AdvQuestBooksItemInfo_OnEnter(this)
	local iconnum = string.gsub(this, "AdvQuestBookMapsItemIcons", "");
	iconnum = tonumber(iconnum);
	GameTooltip:SetOwner(this, "TOPLEFT" and "ANCHOR_LEFT", -6, 0 or "TOPRIGHT" and "ANCHOR_RIGHT", 0, 6);
	GameTooltip:SetText("|cffFFFF00"..AQB_SITEM_DATA[iconnum]["name"].."|r");
	if (AdvQuestBook_Config["settings"][7]["value"]) then
		GameTooltip:AddLine(AQB_ISTRINGS["AQB_COORDS"]..": "..AQB_DecMagic(AQB_SITEM_DATA[iconnum]["x"], 2)..", "..AQB_DecMagic(AQB_SITEM_DATA[iconnum]["y"], 2));
	end
	AdvQuestBook_Checktooltip();
	GameTooltip:Show();
end
--[[ End Shared Support ]]

function AdvQuestBookSnoop_OnLeave(this)
	if (not AdvQuestBook_Config["settings"][2]["value"]) then
		GameTooltip:Hide();
	end
end

function AdvQuestBookSnoop_OnEnter(this)
	GameTooltip:SetOwner(this, "TOPLEFT" and "ANCHOR_LEFT", -6, 0 or "TOPRIGHT" and "ANCHOR_RIGHT", 0, 6);
	GameTooltip:SetText("|cffFFFF00"..AQB_CUR_SNOOP["name"].." "..AQB_ISTRINGS["AQB_TRANSPORT"].."|r");
	if (AdvQuestBook_Config["settings"][5]["value"]) then
		GameTooltip:AddLine(AQB_ISTRINGS["AQB_RECLEVEL"].." "..AQB_CUR_SNOOP["level"].."+");
	end
	if (AdvQuestBook_Config["settings"][4]["value"]) then
		GameTooltip:AddLine("\n|cffFFFF00"..AQB_ISTRINGS["AQB_LINKSTO"]..":|r\n"..AQB_CUR_SNOOP["links"]);
	end
	if (AdvQuestBook_Config["settings"][7]["value"]) then
		GameTooltip:AddLine("\n|cffFFFF00"..AQB_ISTRINGS["AQB_COORDS"]..":|r "..AQB_DecMagic(AQB_CUR_SNOOP["x"], 2)..", "..AQB_DecMagic(AQB_CUR_SNOOP["y"], 2));
	end
	AdvQuestBook_Checktooltip();
	GameTooltip:Show();
end

function AdvQuestBookMapperWorldMap_IconAddToolTip(this, id, appendtext)
	local prim, sec = UnitLevel("player");
	local pdiff;
	local recolor;
	local AQB_QRECLEVEL;
	local AQB_QNAME;
	local AQB_QTEXT;
	local AQB_QNPC;
	local AQB_QCOORDX;
	local AQB_QCOORDY;
	local AQB_QGOLD;
	local AQB_QXP;
	local AQB_QTP;
	if (AdvQuestBook_Config["settings"][1]["value"]) then
		if (not AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[id], true)) then
			if (AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[id]["name"], true)) then
				AQB_QNAME = AQB_ISTRINGS["AQB_UNKNOWNQID"]..": "..id;
			else
				AQB_QNAME = AdvQuestBook_GlobalQuestIndex[id]["name"];
			end
			if (AdvQuestBook_Config["settings"][5]["value"]) then
				AQB_QRECLEVEL = AdvQuestBook_GlobalQuestIndex[id]["info"]["level"];
				pdiff = prim - AQB_QRECLEVEL;
				if (pdiff >= 5) then
					recolor = "00FB00";
				elseif (pdiff >= 0) then
					recolor = "F6FF04";
				else
					recolor = "FF1804";
				end
			end
			if (AdvQuestBook_Config["settings"][4]["value"]) then
				AQB_QTEXT = AdvQuestBook_GlobalQuestIndex[id]["info"]["descshort"];
				AQB_QTEXT = string.gsub(AQB_QTEXT, "0022ee+", "60b2ff");
				AQB_QTEXT = string.gsub(AQB_QTEXT, "118f00+", "59de00");
				AQB_QTEXT = string.gsub(AQB_QTEXT, "ca561f+", "f1cd03");
			end
			AQB_QNPC = AdvQuestBook_GlobalQuestIndex[id]["end"]["npc"];
			if (AdvQuestBook_Config["settings"][7]["value"]) then
				AQB_QCOORDX = AdvQuestBook_GlobalQuestIndex[id]["end"]["x"];
				AQB_QCOORDY = AdvQuestBook_GlobalQuestIndex[id]["end"]["y"];
				AQB_QCOORDX = AQB_DecMagic(AQB_QCOORDX, 2);
				AQB_QCOORDY = AQB_DecMagic(AQB_QCOORDY, 2);
			end
			if (AdvQuestBook_Config["settings"][6]["value"]) then
				AQB_QGOLD = AdvQuestBook_GlobalQuestIndex[id]["rewards"]["Gold"];
				AQB_QXP = AdvQuestBook_GlobalQuestIndex[id]["rewards"]["Exp"];
				AQB_QTP = AdvQuestBook_GlobalQuestIndex[id]["rewards"]["TP"];
			end
			GameTooltip:SetOwner(this, "TOPLEFT" and "ANCHOR_LEFT", -6, 0 or "TOPRIGHT" and "ANCHOR_RIGHT", 0, 6);
			if (not AQB_IsEmpty(appendtext, true)) then
				GameTooltip:SetText("|cffFFFF00"..AQB_QNAME..appendtext.."|r");
			else
				GameTooltip:SetText("|cffFFFF00"..AQB_QNAME.."|r");
			end
			if (AdvQuestBook_Config["settings"][5]["value"]) then
				GameTooltip:AddLine(AQB_ISTRINGS["AQB_RECLEVEL"].." |cff"..recolor..AQB_QRECLEVEL.."+|r");
			end
			if (AdvQuestBook_Config["settings"][4]["value"]) then
				GameTooltip:AddLine("\n"..AQB_QTEXT);
			end
			--[[ Add Items ? ]]
			if (AdvQuestBook_Config["settings"][6]["value"]) then
				GameTooltip:AddLine("\n"..AQB_ISTRINGS["AQB_REWARDS"]..":");
				GameTooltip:AddLine(" ".." ".." ".." "..AQB_ISTRINGS["AQB_XP"]..": "..AQB_QXP);
				GameTooltip:AddLine(" ".." ".." ".." "..AQB_ISTRINGS["AQB_TP"]..": "..AQB_QTP);
				GameTooltip:AddLine(" ".." ".." ".." "..AQB_ISTRINGS["AQB_GOLD"]..": "..AQB_QGOLD);
			end
			if (AdvQuestBook_Config["settings"][7]["value"]) then
				GameTooltip:AddLine("\n"..AQB_ISTRINGS["AQB_COORDS"]..": "..AQB_QCOORDX..", "..AQB_QCOORDY);
			end
			AdvQuestBook_Checktooltip();
			GameTooltip:Show();
		end
	end
end

--[[ Begin Shared Support ]]
function AdvQuestBookMapperWorldMap_sIconAddToolTip(this, id, appendtext)
	local prim, sec = UnitLevel("player");
	local pdiff;
	local recolor;
	local AQB_QRECLEVEL;
	local AQB_QNAME;
	local AQB_QTEXT;
	local AQB_QNPC;
	local AQB_QCOORDX;
	local AQB_QCOORDY;
	local AQB_QGOLD;
	local AQB_QXP;
	local AQB_QTP;
	if (AdvQuestBook_Config["settings"][1]["value"]) then
		if (not AQB_IsEmpty(AdvQuestBook_GlobalSharedIndex[id], true)) then
			if (AQB_IsEmpty(AdvQuestBook_GlobalSharedIndex[id]["name"], true)) then
				AQB_QNAME = AQB_ISTRINGS["AQB_UNKNOWNQID"]..": "..id;
			else
				AQB_QNAME = AdvQuestBook_GlobalSharedIndex[id]["name"];
			end
			if (AdvQuestBook_Config["settings"][5]["value"]) then
				AQB_QRECLEVEL = AdvQuestBook_GlobalSharedIndex[id]["info"]["level"];
				pdiff = prim - AQB_QRECLEVEL;
				if (pdiff >= 5) then
					recolor = "00FB00";
				elseif (pdiff >= 0) then
					recolor = "F6FF04";
				else
					recolor = "FF1804";
				end
			end
			if (AdvQuestBook_Config["settings"][4]["value"]) then
				AQB_QTEXT = AdvQuestBook_GlobalSharedIndex[id]["info"]["descshort"];
				AQB_QTEXT = string.gsub(AQB_QTEXT, "0022ee+", "60b2ff");
				AQB_QTEXT = string.gsub(AQB_QTEXT, "118f00+", "59de00");
				AQB_QTEXT = string.gsub(AQB_QTEXT, "ca561f+", "f1cd03");
			end
			AQB_QNPC = AdvQuestBook_GlobalSharedIndex[id]["end"]["npc"];
			if (AdvQuestBook_Config["settings"][7]["value"]) then
				AQB_QCOORDX = AdvQuestBook_GlobalSharedIndex[id]["end"]["x"];
				AQB_QCOORDY = AdvQuestBook_GlobalSharedIndex[id]["end"]["y"];
				AQB_QCOORDX = AQB_DecMagic(AQB_QCOORDX, 2);
				AQB_QCOORDY = AQB_DecMagic(AQB_QCOORDY, 2);
			end
			if (AdvQuestBook_Config["settings"][6]["value"]) then
				AQB_QGOLD = AdvQuestBook_GlobalSharedIndex[id]["rewards"]["Gold"];
				AQB_QXP = AdvQuestBook_GlobalSharedIndex[id]["rewards"]["Exp"];
				AQB_QTP = AdvQuestBook_GlobalSharedIndex[id]["rewards"]["TP"];
			end
			GameTooltip:SetOwner(this, "TOPLEFT" and "ANCHOR_LEFT", -6, 0 or "TOPRIGHT" and "ANCHOR_RIGHT", 0, 6);
			if (not AQB_IsEmpty(appendtext, true)) then
				GameTooltip:SetText("|cffFFFF00"..AQB_QNAME..appendtext.."|r");
			else
				GameTooltip:SetText("|cffFFFF00"..AQB_QNAME.."|r");
			end
			if (AdvQuestBook_Config["settings"][5]["value"]) then
				GameTooltip:AddLine(AQB_ISTRINGS["AQB_RECLEVEL"].." |cff"..recolor..AQB_QRECLEVEL.."+|r");
			end
			if (AdvQuestBook_Config["settings"][4]["value"]) then
				GameTooltip:AddLine("\n"..AQB_QTEXT);
			end
			--[[ Add Items ? ]]
			if (AdvQuestBook_Config["settings"][6]["value"]) then
				GameTooltip:AddLine("\n"..AQB_ISTRINGS["AQB_REWARDS"]..":");
				GameTooltip:AddLine(" ".." ".." ".." "..AQB_ISTRINGS["AQB_XP"]..": "..AQB_QXP);
				GameTooltip:AddLine(" ".." ".." ".." "..AQB_ISTRINGS["AQB_TP"]..": "..AQB_QTP);
				GameTooltip:AddLine(" ".." ".." ".." "..AQB_ISTRINGS["AQB_GOLD"]..": "..AQB_QGOLD);
			end
			if (AdvQuestBook_Config["settings"][7]["value"]) then
				GameTooltip:AddLine("\n"..AQB_ISTRINGS["AQB_COORDS"]..": "..AQB_QCOORDX..", "..AQB_QCOORDY);
			end
			GameTooltip:AddLine("\n"..AQB_SharedQuests[AQB_LAST_SHARE]["name"].."\'s "..AQB_ISTRINGS["AQB_QUEST"]);
			AdvQuestBook_Checktooltip();
			GameTooltip:Show();
		end
	end
end

function AdvQuestBookMapperWorldMapsIndex_IconOnEnter(this)
	local AQB_MSCALE = WorldMapViewFrame:GetScale();
	local AQB_MWIDTH = WorldMapViewFrame:GetWidth();
	local AQB_MHEIGHT = WorldMapViewFrame:GetHeight();
	local AQB_MAPID = WorldMapFrame.mapID;
	AQB_LAST_MAP = AQB_MAPID;
	local AQB_ICO_NUM = string.gsub(this, "AdvQuestBookMapSharedIcons", "");
	local AQB_QUESTNUM;
	local AQB_I, AQB_V;
	local AQB_CLOCS;
	local AQB_MPX;
	local AQB_MPY;
	local AQB_MID;
	local AQB_IDX = 1;
	local AQB_IDXX = 0;
	local i;
	if (AQB_SOPEN_ITEMS > 0) then
		for i = 1, AQB_SOPEN_ITEMS do
			icon = getglobal("AdvQuestBookMapsItemIcons"..i);
			icon:Hide();
			AQB_SOPEN_ITEMS = 0;
		end
	end
	AQB_ICO_NUM = tonumber(AQB_ICO_NUM);
	if (not AQB_IsEmpty(AQB_SITEM_INDEX[AQB_ICO_NUM], true)) then
		AQB_QUESTNUM = AQB_SITEM_INDEX[AQB_ICO_NUM];
		if (not AQB_IsEmpty(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["items"], true)) then
			for AQB_I, AQB_V in pairs(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["items"]) do
				AQB_CLOCS = table.getn(AQB_V["locations"]);
				if (AQB_CLOCS > 1) then
					for i = 2, AQB_CLOCS do
						AQB_MID = AQB_V["locations"][i]["mapid"];
						AQB_MPX = AQB_V["locations"][i]["x"];
						AQB_MPY = AQB_V["locations"][i]["y"];
						if (AQB_MID == AQB_MAPID) then
							AQB_IDXX = AQB_IDXX + 1;
							icon = getglobal("AdvQuestBookMapsItemIcons"..AQB_IDXX);
							icon:ClearAllAnchors();
							icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon.mid = AQB_MID;
							icon.x = AQB_MPX;
							icon.y = AQB_MPY;
							icon:Show();
							AQB_SITEM_DATA[AQB_IDXX] = {
								["name"] = AQB_I,
								["mapid"] = AQB_MID,
								["x"] = AQB_MPX,
								["y"] = AQB_MPY,
							};
							AQB_SOPEN_ITEMS = AQB_SOPEN_ITEMS + 1;
						end
					end
				else
					AQB_MID = AQB_V["locations"][1]["mapid"];
					AQB_MPX = AQB_V["locations"][1]["x"];
					AQB_MPY = AQB_V["locations"][1]["y"];
					if (AQB_MID == AQB_MAPID) then
						AQB_IDXX = AQB_IDXX + 1;
						icon = getglobal("AdvQuestBookMapsItemIcons"..AQB_IDXX);
						icon:ClearAllAnchors();
						icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon.mid = AQB_MID;
						icon.x = AQB_MPX;
						icon.y = AQB_MPY;
						icon:Show();
						AQB_SITEM_DATA[AQB_IDXX] = {
							["name"] = AQB_I,
							["mapid"] = AQB_MID,
							["x"] = AQB_MPX,
							["y"] = AQB_MPY,
						};
						AQB_SOPEN_ITEMS = AQB_SOPEN_ITEMS + 1;
					end
				end
			end
		elseif (not AQB_IsEmpty(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"], true)) then
			AQB_CLOCS = table.getn(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"]);
			if (not AQB_IsEmpty(AQB_CLOCS, true)) then
				for i = 1, AQB_CLOCS do
					AQB_MID = AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"][i]["mapid"];
					AQB_MPX = AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"][i]["x"];
					AQB_MPY = AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"][i]["y"];
					if (AQB_MID == AQB_MAPID) then
						AQB_IDXX = AQB_IDXX + 1;
						icon = getglobal("AdvQuestBookMapsItemIcons"..AQB_IDXX);
						icon:ClearAllAnchors();
						icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon.mid = AQB_MID;
						icon.x = AQB_MPX;
						icon.y = AQB_MPY;
						icon:Show();
						AQB_SITEM_DATA[AQB_IDXX] = {
							["name"] = AQB_I,
							["mapid"] = AQB_MID,
							["x"] = AQB_MPX,
							["y"] = AQB_MPY,
						};
						AQB_SOPEN_ITEMS = AQB_SOPEN_ITEMS + 1;
					end
				end
			end
		end
		AdvQuestBookMapperWorldMap_sIconAddToolTip(this, AQB_QUESTNUM, 0);
	end
end
--[[ End Shared Support ]]

function AdvQuestBookMapperWorldMapIndex_IconOnEnter(this)
	local AQB_MSCALE = WorldMapViewFrame:GetScale();
	local AQB_MWIDTH = WorldMapViewFrame:GetWidth();
	local AQB_MHEIGHT = WorldMapViewFrame:GetHeight();
	local AQB_MAPID = WorldMapFrame.mapID;
	AQB_LAST_MAP = AQB_MAPID;
	local AQB_ICO_NUM = string.gsub(this, "AdvQuestBookMapIndexIcons", "");
	local AQB_QUESTNUM;
	local AQB_I, AQB_V;
	local AQB_CLOCS;
	local AQB_MPX;
	local AQB_MPY;
	local AQB_MID;
	local AQB_IDX = 1;
	local AQB_IDXX = 0;
	local i;
	if (AQB_OPEN_ITEMS > 0) then
		for i = 1, AQB_OPEN_ITEMS do
			icon = getglobal("AdvQuestBookMapItemIcons"..i);
			icon:Hide();
			AQB_OPEN_ITEMS = 0;
		end
	end
	AQB_ICO_NUM = tonumber(AQB_ICO_NUM);
	if (not AQB_IsEmpty(AQB_ITEM_INDEX[AQB_ICO_NUM], true)) then
		AQB_QUESTNUM = AQB_ITEM_INDEX[AQB_ICO_NUM];
		if (not AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["items"], true)) then
			for AQB_I, AQB_V in pairs(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["items"]) do
				AQB_CLOCS = table.getn(AQB_V["locations"]);
				if (AQB_CLOCS > 1) then
					for i = 2, AQB_CLOCS do
						AQB_MID = AQB_V["locations"][i]["mapid"];
						AQB_MPX = AQB_V["locations"][i]["x"];
						AQB_MPY = AQB_V["locations"][i]["y"];
						if (AQB_MID == AQB_MAPID) then
							AQB_IDXX = AQB_IDXX + 1;
							icon = getglobal("AdvQuestBookMapItemIcons"..AQB_IDXX);
							icon:ClearAllAnchors();
							icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon.mid = AQB_MID;
							icon.x = AQB_MPX;
							icon.y = AQB_MPY;
							icon:Show();
							AQB_ITEM_DATA[AQB_IDXX] = {
								["name"] = AQB_I,
								["mapid"] = AQB_MID,
								["x"] = AQB_MPX,
								["y"] = AQB_MPY,
							};
							AQB_OPEN_ITEMS = AQB_OPEN_ITEMS + 1;
						end
					end
				else
					AQB_MID = AQB_V["locations"][1]["mapid"];
					AQB_MPX = AQB_V["locations"][1]["x"];
					AQB_MPY = AQB_V["locations"][1]["y"];
					if (AQB_MID == AQB_MAPID) then
						AQB_IDXX = AQB_IDXX + 1;
						icon = getglobal("AdvQuestBookMapItemIcons"..AQB_IDXX);
						icon:ClearAllAnchors();
						icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon.mid = AQB_MID;
						icon.x = AQB_MPX;
						icon.y = AQB_MPY;
						icon:Show();
						AQB_ITEM_DATA[AQB_IDXX] = {
							["name"] = AQB_I,
							["mapid"] = AQB_MID,
							["x"] = AQB_MPX,
							["y"] = AQB_MPY,
						};
						AQB_OPEN_ITEMS = AQB_OPEN_ITEMS + 1;
					end
				end
			end
		elseif (not AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"], true)) then
			AQB_CLOCS = table.getn(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"]);
			if (not AQB_IsEmpty(AQB_CLOCS, true)) then
				for i = 1, AQB_CLOCS do
					AQB_MID = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"][i]["mapid"];
					AQB_MPX = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"][i]["x"];
					AQB_MPY = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"][i]["y"];
					if (AQB_MID == AQB_MAPID) then
						AQB_IDXX = AQB_IDXX + 1;
						icon = getglobal("AdvQuestBookMapItemIcons"..AQB_IDXX);
						icon:ClearAllAnchors();
						icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
						icon.mid = AQB_MID;
						icon.x = AQB_MPX;
						icon.y = AQB_MPY;
						icon:Show();
						AQB_ITEM_DATA[AQB_IDXX] = {
							["name"] = AQB_I,
							["mapid"] = AQB_MID,
							["x"] = AQB_MPX,
							["y"] = AQB_MPY,
						};
						AQB_OPEN_ITEMS = AQB_OPEN_ITEMS + 1;
					end
				end
			end
		end
		AdvQuestBookMapperWorldMap_IconAddToolTip(this, AQB_QUESTNUM, 0);
	end
end

function AdvQuestBookMapperWorldMap_IconOnEnter(this)
	local iconnum = string.gsub(this, "AdvQuestBookMapFinIcons", "");
	iconnum = tonumber(iconnum);
	AQB_QUESTNUM = AQB_FIN_MAPLIST[iconnum];
	if (not AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM], true)) then
		AdvQuestBookMapperWorldMap_IconAddToolTip(this, AQB_QUESTNUM, AQB_SSTRINGS["AQB_COMPLETE1_TEXT"]);
	end
end

function AdvQuestBook_Mapper()
	local i;
	local AQB_MSCALE = WorldMapViewFrame:GetScale();
	local AQB_MWIDTH = WorldMapViewFrame:GetWidth();
	local AQB_MHEIGHT = WorldMapViewFrame:GetHeight();
	local AQB_MAPID = WorldMapFrame.mapID;
	AQB_LAST_MAP = AQB_MAPID;
	local AQB_TFIN = table.getn(AQB_FIN_MAPLIST);
	local AQB_TINC = table.getn(AQB_INC_MAPLIST);
	local AQB_I, AQB_V;
	local AQB_QUESTNUM;
	local AQB_MPNPC;
	local AQB_MPNPCID;
	local AQB_MID;
	local AQB_MPX;
	local AQB_MPY;
	local AQB_IDX;
	local AQB_IDXX = 0;
	local AQB_LOCC;
	local AQB_GETLOC;
	for AQB_IDX = 1, AQB_TFIN do
		if (not AQB_IsEmpty(AQB_FIN_MAPLIST[AQB_IDX], true)) then
			AQB_QUESTNUM = AQB_FIN_MAPLIST[AQB_IDX];
			AQB_MPNPC = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["end"]["npc"];
			AQB_MPNPCID = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["end"]["npcid"];
			AQB_MID = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["end"]["mapid"];
			AQB_MPX = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["end"]["x"];
			AQB_MPY = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["end"]["y"];
			if (AQB_MID == AQB_MAPID) then
				icon = getglobal("AdvQuestBookMapFinIcons"..AQB_IDX);
				icon:ClearAllAnchors();
				icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
				icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
				icon.mid = AQB_MID;
				icon.x = AQB_MPX;
				icon.y = AQB_MPY;
				icon.enpcid = AQB_MPNPCID;
				icon:Show();
				AQB_FINICON_COUNT = AQB_FINICON_COUNT + 1;
			end
		end
	end
	for AQB_IDX = 1, AQB_TINC do
		if (not AQB_IsEmpty(AQB_INC_MAPLIST[AQB_IDX], true)) then
			AQB_QUESTNUM = AQB_INC_MAPLIST[AQB_IDX];
			if (not AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["items"], true)) then
				for AQB_I, AQB_V in pairs(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["items"]) do
					AQB_MID = AQB_V["locations"][1]["mapid"];
					AQB_MPX = AQB_V["locations"][1]["x"];
					AQB_MPY = AQB_V["locations"][1]["y"];
					if (not AQB_IsEmpty(AQB_MID, true) and not AQB_IsEmpty(AQB_MPX, true) and not AQB_IsEmpty(AQB_MPY, true)) then
						if (AQB_MID == AQB_MAPID) then
							AQB_IDXX = AQB_IDXX + 1;
							icon = getglobal("AdvQuestBookMapIndexIcons"..AQB_IDXX);
							icon:ClearAllAnchors();
							icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon.mid = AQB_MID;
							icon.x = AQB_MPX;
							icon.y = AQB_MPY;
							icon:Show();
							AQB_ITEM_INDEX[AQB_IDXX] = AQB_QUESTNUM;
							AQB_INCICON_COUNT = AQB_INCICON_COUNT + 1;
						end
					end
				end
			end
			if (AQB_ITEM_INDEX[AQB_IDXX] ~= AQB_QUESTNUM) then
				if (not AQB_IsEmpty(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"], true)) then
					local AQB_LOCC = table.getn(AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"]);
					for AQB_GETLOC = 1, AQB_LOCC do
						AQB_MID = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"][AQB_GETLOC]["mapid"];
						AQB_MPX = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"][AQB_GETLOC]["x"];
						AQB_MPY = AdvQuestBook_GlobalQuestIndex[AQB_QUESTNUM]["locations"][AQB_GETLOC]["y"];
						if (not AQB_IsEmpty(AQB_MID, true) and not AQB_IsEmpty(AQB_MPX, true) and not AQB_IsEmpty(AQB_MPY, true)) then
							if (AQB_MID == AQB_MAPID) then
								AQB_IDXX = AQB_IDXX + 1;
								icon = getglobal("AdvQuestBookMapIndexIcons"..AQB_IDXX);
								icon:ClearAllAnchors();
								icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
								icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
								icon.mid = AQB_MID;
								icon.x = AQB_MPX;
								icon.y = AQB_MPY;
								icon:Show();
								AQB_ITEM_INDEX[AQB_IDXX] = AQB_QUESTNUM;
								AQB_INCICON_COUNT = AQB_INCICON_COUNT + 1;
								break;
							end
						end
					end
				end
			end
		end
	end
	--[[ Begin Snoop Transport Support ]]
	if (AdvQuestBook_Config["settings"][11]["value"]) then
		local AQB_SNOOP_COUNT = table.getn(AQB_SNOOP_TRANSPORT);
		local prim, sec = UnitLevel("player");
		local icotype;
		local tcount;
		local text = "";
		for i = 1, AQB_SNOOP_COUNT do
			if (AQB_SNOOP_TRANSPORT[i]["mapid"] == AQB_MAPID) then
				if (prim < AQB_SNOOP_TRANSPORT[i]["level"]) then
					icotype = "b";
				else
					icotype = "a";
				end
				AQB_MPX = AQB_SNOOP_TRANSPORT[i]["x"];
				AQB_MPY = AQB_SNOOP_TRANSPORT[i]["y"];
				AQB_CUR_SNOOP = {
					["name"] = AQB_SNOOP_TRANSPORT[i]["name"],
					["mapid"] = AQB_MAPID,
					["level"] = AQB_SNOOP_TRANSPORT[i]["level"],
					["links"] = AQB_SNOOP_TRANSPORT[i]["links"],
					["x"] = AQB_MPX,
					["y"] = AQB_MPY,
				};
				icon = getglobal("AdvQuestBookSnoopIcons"..icotype);
				icon:ClearAllAnchors();
				icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
				icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
				icon.mid = AQB_MAPID;
				icon.x = AQB_MPX;
				icon.y = AQB_MPY;
				icon:Show();
			end
		end
	end
	--[[ End Snoop Transport Support ]]
	--[[ Begin Elite Skill Trainers Support ]]
	if (AdvQuestBook_Config["settings"][12]["value"]) then
		if (not AQB_IsEmpty(AQB_ELITE_TRAINERS[AQB_MAPID], true)) then
			local tcount = table.getn(AQB_ELITE_TRAINERS[AQB_MAPID]);
			for i = 1, tcount do
				AQB_MPX = AQB_ELITE_TRAINERS[AQB_MAPID][i]["x"];
				AQB_MPY = AQB_ELITE_TRAINERS[AQB_MAPID][i]["y"];
				icon = getglobal("AdvQuestBookEliteTrainerIcons"..i);
				icon:ClearAllAnchors();
				icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
				icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
				icon.mid = AQB_MAPID;
				icon.x = AQB_MPX;
				icon.y = AQB_MPY;
				icon.name = AQB_ELITE_TRAINERS[AQB_MAPID][i]["name"];
				icon.items = AQB_ELITE_TRAINERS[AQB_MAPID][i]["items"];
				icon:Show();
			end
		end
	end
	--[[ End Elite Skill Trainers Support ]]
	--[ Begin Shared Support ]]
	if (AdvQuestBook_Config["settings"][13]["value"]) then
		if (AQB_LAST_SHARE > 0) then
			local AQB_SQC = table.getn(AQB_SharedQuests[AQB_LAST_SHARE]["quests"]);
			local AQB_IDXX = 0;
			for AQB_IDX = 1, AQB_SQC do
				AQB_QUESTNUM = AQB_SharedQuests[AQB_LAST_SHARE]["quests"][AQB_IDX];
				AQB_QUESTNUM = tonumber(AQB_QUESTNUM);
				if (not AQB_IsEmpty(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["items"], true)) then
					for AQB_I, AQB_V in pairs(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["items"]) do
						AQB_MID = AQB_V["locations"][1]["mapid"];
						AQB_MPX = AQB_V["locations"][1]["x"];
						AQB_MPY = AQB_V["locations"][1]["y"];
						if (AQB_MID == AQB_MAPID) then
							AQB_IDXX = AQB_IDXX + 1;
							icon = getglobal("AdvQuestBookMapSharedIcons"..AQB_IDXX);
							icon:ClearAllAnchors();
							icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
							icon.mid = AQB_MID;
							icon.x = AQB_MPX;
							icon.y = AQB_MPY;
							icon:Show();
							AQB_SITEM_INDEX[AQB_IDXX] = AQB_QUESTNUM;
						end
					end
				end
				if (AQB_SITEM_INDEX[AQB_IDXX] ~= AQB_QUESTNUM) then
					if (not AQB_IsEmpty(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"], true)) then
						local AQB_LOCC = table.getn(AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"]);
						for AQB_GETLOC = 1, AQB_LOCC do
							AQB_MID = AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"][AQB_GETLOC]["mapid"];
							AQB_MPX = AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"][AQB_GETLOC]["x"];
							AQB_MPY = AdvQuestBook_GlobalSharedIndex[AQB_QUESTNUM]["locations"][AQB_GETLOC]["y"];
							if (AQB_MID == AQB_MAPID) then
								AQB_IDXX = AQB_IDXX + 1;
								icon = getglobal("AdvQuestBookMapSharedIcons"..AQB_IDXX);
								icon:ClearAllAnchors();
								icon:SetPos(AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
								icon:SetAnchor("CENTER", "TOPLEFT", "WorldMapViewFrame", AQB_MPX * AQB_MWIDTH, AQB_MPY * AQB_MHEIGHT);
								icon.mid = AQB_MID;
								icon.x = AQB_MPX;
								icon.y = AQB_MPY;
								icon:Show();
								AQB_SITEM_INDEX[AQB_IDXX] = AQB_QUESTNUM;
								break;
							end
						end
					end
				end
			end
		end
	end
	--[ End Shared Support ]]
end

function AdvQuestBook_Gmap()
	local AQB_TQUESTS = GetNumQuestBookButton_QuestBook();
	local AQB_MID = WorldMapFrame.mapID;
	AQB_LAST_MAP = AQB_MID;
	local AQB_COUNTER;
	local AQB_MAPPER_QC;
	local AQB_MAPPED;
	local AQB_QUESTNAME;
	local AQB_QUESTID;
	local i;
	local z;
	local idx, catid, name, track, level, daily;
	for i = 1, AQB_TQUESTS do
		idx, catid, name, track, level, daily = GetQuestInfo(i);
		if (string.match(name, AQB_SSTRINGS["AQB_QUEST_COMPLETE1"])) then
			AQB_MAPPER_QC = true;
		else 
			AQB_MAPPER_QC = false;
		end
		AQB_QUESTNAME = string.gsub(name, AQB_SSTRINGS["AQB_QUEST_COMPLETE1"], "");
		AQB_QUESTNAME = string.gsub(AQB_QUESTNAME, "%-+", "");
		AQB_QUESTNAME = string.gsub(AQB_QUESTNAME, "%s%s+", " ");
		AQB_MAPPED = false;
		if (not AQB_IsEmpty(AQBQbyName[AQB_QUESTNAME], true)) then
			AQB_QUESTID = AQBQbyName[AQB_QUESTNAME];
			if (AQB_MAPPER_QC) then
				for z = 1, AQB_TQUESTS do
					if (AQB_IsEmpty(AQB_FIN_MAPLIST[z], true)) then
						break;
					else
						if (not AQB_IsEmpty(AQB_FIN_MAPLIST[z], true)) then
							if (AQB_FIN_MAPLIST[z] == AQB_QUESTID) then
								AQB_MAPPED = true;
								break;
							end
						end
					end
				end
				if (not AQB_MAPPED) then
					AQB_COUNTER = table.getn(AQB_FIN_MAPLIST);
					if (AQB_COUNTER < 30) then
						AQB_FIN_MAPLIST[AQB_COUNTER + 1] = AQB_QUESTID;
					end
				end
			else
				for z = 1, AQB_TQUESTS do
					if (AQB_IsEmpty(AQB_INC_MAPLIST[z], true)) then
						break;
					else
						if (not AQB_IsEmpty(AQB_INC_MAPLIST[z], true)) then
							if (AQB_INC_MAPLIST[z] == AQB_QUESTID) then
								AQB_MAPPED = true;
								break;
							end
						end
					end
				end
				if (not AQB_MAPPED) then
					AQB_COUNTER = table.getn(AQB_INC_MAPLIST);
					if (AQB_COUNTER < 30) then
						AQB_INC_MAPLIST[AQB_COUNTER + 1] = AQB_QUESTID;
					end
				end
			end
		end
	end
	AdvQuestBook_Mapper();
end

function AdvQuestBook_OnUpdate(this)
	if (WorldMapFrame:IsVisible()) then
		if (not AQB_ENABLE_ICONS) then
			AdvQuestBook_ClearIcons();
			AQB_ENABLE_ICONS = true;
			AdvQuestBook_SetQuestIndexes();
			AdvQuestBook_Gmap();
		end
		if (WorldMapFrame.mapID ~= AQB_LAST_MAP) then
			AdvQuestBook_ClearIcons();
			AQB_ENABLE_ICONS = true;
			AdvQuestBook_Gmap();
		end
	elseif (not WorldMapFrame:IsVisible()) then
		if (AQB_ENABLE_ICONS) then
			AdvQuestBook_ClearIcons();
			AdvQuestBook_GlobalQuestIndex = {};
		end
	end
end
